CC_DIAMONDS = 1
CC_CLUBS	= 2
CC_HEARTS	= 3
CC_SPADES	= 4
CC_WANG	= 5

--黑 3
--红 2
--梅 1
--方 0
local TPoker = class("TPoker", function()
	local layer = CCLayerColor:create(cc.c4b(250, 10, 10, 0))--cc.Layer:create()
	return layer
end)


function TPoker:ctor(...)
	self:init(...)
end

function TPoker:init()
	--背面
	local pkbm = CCSpriteFrameCache:getInstance():getSpriteFrame("poker_bk.png")
	self.BackSpr = CCSprite:createWithSpriteFrame(pkbm)
	self:addChild(self.BackSpr, 1)
	
	--正面
	self.FronFace = cc.Layer:create():addTo(self)
	self.FronFace:setAnchorPoint(0, 0)
	self:setAnchorPoint(0, 0)
	
	self:setScale(scale or 0.38)
	
end

function TPoker:SetCard(paiColor, paiNum)
	self.FronFace:removeAllChildren(true)
	
	local zmbj = CCSpriteFrameCache:getInstance():getSpriteFrame("poker_zmbj.png")
	self.FrontSpr = CCSprite:createWithSpriteFrame(zmbj):addTo(self.FronFace)
	
	local name = ""
	if(paiColor == CC_SPADES) then
		name = "poker_" .. paiNum .. "1.png"
		local _color = CCSpriteFrameCache:getInstance():getSpriteFrame("poker_mht.png")
		CCSprite:createWithSpriteFrame(_color):move(- 90, 25):addTo(self.FronFace):setScale(0.7)
	elseif(paiColor == CC_HEARTS) then
		name = "poker_" .. paiNum .. "2.png"
		local _color = CCSpriteFrameCache:getInstance():getSpriteFrame("poker_mhx.png")
		CCSprite:createWithSpriteFrame(_color):move(- 90, 25):addTo(self.FronFace):setScale(0.7)
	elseif(paiColor == CC_CLUBS) then
		name = "poker_" .. paiNum .. "1.png"
		local _color = CCSpriteFrameCache:getInstance():getSpriteFrame("poker_myh.png") --poker_myh
		CCSprite:createWithSpriteFrame(_color):move(- 90, 25):addTo(self.FronFace):setScale(0.7)
	elseif(paiColor == CC_DIAMONDS) then
		name = "poker_" .. paiNum .. "2.png"
		local _color = CCSpriteFrameCache:getInstance():getSpriteFrame("poker_mfk.png") --poker_mfk
		CCSprite:createWithSpriteFrame(_color):move(- 90, 25):addTo(self.FronFace):setScale(0.7)
	elseif(paiColor == CC_WANG) then
		if paiNum == 14 then
			name = "poker_141.png"
			local _color = CCSpriteFrameCache:getInstance():getSpriteFrame("poker_141_bk.png") --poker_mfk
			CCSprite:createWithSpriteFrame(_color):move(0, 0):addTo(self.FronFace):setScale(0.7)
		elseif paiNum == 15 then
			name = "poker_142.png"
			local _color = CCSpriteFrameCache:getInstance():getSpriteFrame("poker_142_bk.png") --poker_mfk
			CCSprite:createWithSpriteFrame(_color):move(0, 0):addTo(self.FronFace):setScale(0.7)
		end
		
		
	else
		assert(false)
	end
	
	if(paiNum == 11) then
		if(paiColor == CC_SPADES or paiColor == CC_CLUBS) then
			local _color = CCSpriteFrameCache:getInstance():getSpriteFrame("poker_111_bk.png")
			CCSprite:createWithSpriteFrame(_color):move(70, - 92):addTo(self.FronFace):setScale(0.7)
		else
			local _color = CCSpriteFrameCache:getInstance():getSpriteFrame("poker_112_bk.png")
			CCSprite:createWithSpriteFrame(_color):move(70, - 92):addTo(self.FronFace):setScale(0.7)
		end
	elseif(paiNum == 12) then
		if(paiColor == CC_SPADES or paiColor == CC_CLUBS) then
			local _color = CCSpriteFrameCache:getInstance():getSpriteFrame("poker_121_bk.png")
			CCSprite:createWithSpriteFrame(_color):move(70, - 92):addTo(self.FronFace):setScale(0.7)
		else
			local _color = CCSpriteFrameCache:getInstance():getSpriteFrame("poker_122_bk.png")
			CCSprite:createWithSpriteFrame(_color):move(70, - 92):addTo(self.FronFace):setScale(0.7)
		end
	elseif(paiNum == 13) then
		if(paiColor == CC_SPADES or paiColor == CC_CLUBS) then
			local _color = CCSpriteFrameCache:getInstance():getSpriteFrame("poker_131_bk.png")
			CCSprite:createWithSpriteFrame(_color):move(70, - 92):addTo(self.FronFace):setScale(0.7)
		else
			local _color = CCSpriteFrameCache:getInstance():getSpriteFrame("poker_132_bk.png")
			CCSprite:createWithSpriteFrame(_color):move(70, - 92):addTo(self.FronFace):setScale(0.7)
		end
	end
	
	local _num = CCSpriteFrameCache:getInstance():getSpriteFrame(name)
	if _num then
		if paiNum == 14 or paiNum == 15 then
			CCSprite:createWithSpriteFrame(_num):move(- 110, 40):addTo(self.FronFace):setScale(0.866)
		else
			CCSprite:createWithSpriteFrame(_num):move(- 95, 120):addTo(self.FronFace):setScale(0.866)
		end
		
		
	end
end

function TPoker:GetOpenAction(_duration)
	local duration = _duration or 0.15
	local orbitFront = cc.OrbitCamera:create(duration, 1, 0, - 270, - 90, 0, 0)
	local orbitBack = cc.OrbitCamera:create(duration, 1, 0, 0, - 90, 0, 0)
	return cc.TargetedAction:create(self.BackSpr, cc.Sequence:create(
	cc.Sequence:create(cc.Show:create(), orbitBack, cc.Hide:create(),
	cc.TargetedAction:create(self.FronFace, cc.Sequence:create(cc.Show:create(), orbitFront)))))
end

function TPoker:GetOpenAction2(_duration)
	local duration = _duration or 0.3
	local orbitFront = cc.OrbitCamera:create(duration, 1, 0, - 270, - 90, 2, 0)
	local orbitBack = cc.OrbitCamera:create(duration, 1, 0, 0, - 90, 2, 0)

	local scaleTo = cc.ScaleTo:create(0.4,1.3)
	
	local delay = cc.DelayTime:create(0.2)
	local callback1 = cc.CallFunc:create(
		function()
			self.FronFace:setScale(1.3)
		end
	)
	local callback2 = cc.CallFunc:create(
		function()
			self.FronFace:runAction(cc.ScaleTo:create(0.15,1))
		end
	)

	return cc.TargetedAction:create(self.BackSpr, cc.Sequence:create(scaleTo,
	cc.Sequence:create( cc.Show:create(), orbitBack, cc.Hide:create(),callback1,
	cc.TargetedAction:create(self.FronFace, cc.Sequence:create(cc.Show:create(), orbitFront,delay,callback2)))))
end

function TPoker:open(bAnimate)
	if bAnimate then
		self.FronFace:setVisible(false)
		
		self.FronFace:stopAllActions()
		self.BackSpr:stopAllActions()
		self:stopAllActions()
		self:runAction(self:GetOpenAction())
	else
		self.FronFace:setVisible(true)
		self.BackSpr:setVisible(false)
	end
end

function TPoker:open2(bAnimate)
	if bAnimate then
		self.FronFace:setVisible(false)
		
		self.FronFace:stopAllActions()
		self.BackSpr:stopAllActions()
		self:stopAllActions()
		self:runAction(self:GetOpenAction2())
	else
		self.FronFace:setVisible(true)
		self.BackSpr:setVisible(false)
	end
end


function TPoker:close()
	self.FronFace:removeAllChildren(true)
	
	self.BackSpr:setVisible(true)
end

return TPoker 